formatstr = input()
N = int(input())

d = {}
length = len(formatstr)
for i in range(0,length):
    if formatstr[i] == ':':
        d[formatstr[i - 1]] = True
    else:
        if i == length - 1 or formatstr[i + 1] != ':':
            d[formatstr[i]] = False


for i in range(1,N + 1):
    line = input()
    lines = line.split()
    nodes = {}
    hasadd=0

    length_lines = len(lines)
    j=1
    while j<length_lines:
        now = lines[j]
        if now[0] == '-' and len(now) == 2:
            if j + 1 < length_lines:
                if d.get(now[1]) is None:
                    break
                elif d[now[1]] == True:
                    nodes[now[1]] = lines[j + 1]
                    j+=1
                else:
                    nodes[now[1]] = None
            elif (d.get(now[1]) is None) == False and d[now[1]] == False:
                nodes[now[1]] = None
            else:
                break
        else:
            break
        j+=1

    keys = nodes.keys()
    keys = sorted(keys)

    print("Case " + str(i) + ":",end="")
    for key in keys:
        if d[key] == True:
            print(" -" + key + " " + nodes[key],end="")
        else:
            print(" -" + key,end="")
    print("")